<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * Subclass for performing query and update operations on the 'users' table.
 *
 * 
 *
 * @package lib.model
 */ 
class UserPeer extends BaseUserPeer {
	
	public static $networks = array(
		1 => 'Harvard',
		2 => 'Brown',
		3 => 'MIT',
		4 => 'Yale',
		5 => 'Princeton',
		6 => 'Columbia',
		7 => 'Harvard Law',
		8 => 'Harvard Business School',
		10 => 'Other'
	); 
	
//	public static $facebook_fields = array(
//		'first_name' => false,
//		'last_name' => false,
//		'city' => true,
//		'coutry' => true,
//		'network_id' => false,
//		'network_name' => false,
//		'standing' => false,
//		'about_me' => false,
//		'website' => true,
//		'favorite_books' => false,
//	);
	
	public static function retrieveByEmail($email_address) {
		$c = new Criteria();
		$email_criterion = $c->getNewCriterion(UserPeer::EMAIL, $email_address, Criteria::EQUAL);
		
		// add to the Criteria
		$c->add($email_criterion);
		
		// perform the select query and return a single user object if it exists
		return UserPeer::doSelectOne($c);
	}
	
	/**
	 * Retrieves a user based on a given username or email and password combination
	 * Does a SELECT * FROM users WHERE email = :login_name or username = :login_name and password = :password
	 * @param $login_name, an email address or username
	 * @param $password, the md5 password
	 * @return User or null if not found
	 */
	public static function retrieveByEmailOrUsernameAndPassword($login_name, $password) {
		$c = new Criteria();
		$email_criterion = $c->getNewCriterion(UserPeer::EMAIL, $login_name, Criteria::EQUAL);
		$username_criterion = $c->getNewCriterion(UserPeer::USERNAME, $login_name, Criteria::EQUAL);
		
		// combine the username criterion to the email criterion
		$email_criterion->addOr($username_criterion);
		
		// add to the Criteria
		$c->add($email_criterion);

		// add password to the criteria
		$c->add(UserPeer::MD5_PASSWORD, $password, Criteria::EQUAL);
		
		// perform the select query and return a single user object if it exists
		return UserPeer::doSelectOne($c);
	}
	
	/**
	 * Retrieves a user based on a given user id
	 * Does a SELECT * FROM users WHERE id = :id
	 * @param $id
	 * @return User or null if not found
	 */
	public static function retrieveById($id) {
		return UserPeer::retrieveByPK($id);
	}
	
	public static function retrieveByFacebookId($facebook_id) {
		$c = new Criteria();
		$facebook_criterion = $c->getNewCriterion(UserPeer::FACEBOOK_ID, $facebook_id, Criteria::EQUAL);
		$c->add($facebook_criterion);
		return UserPeer::doSelectOne($c);
	}
	
	/* Added by CLH. Function specific to Group Messaging */
	
	public static function groupsToUsers($group_id_array){
		$q= new Criteria();
		$q->add($q->getNewCriterion(GroupMemberPeer::GROUP_ID, 'False', Criteria::EQUAL));
		foreach($group_id_array as $key => $group_id){
			$q->addOr($q->getNewCriterion(GroupMemberPeer::GROUP_ID, $group_id , Criteria::EQUAL));
		} 
		$member_links = GroupMemberPeer::doSelect($q);
		$user_id_array = array();
		foreach($member_links as $key => $member){
			$id = $member->getUserId();
			if(!in_array($id, $user_id_array)){ //prevent duplicates
				$user_id_array[] = $id;
			}
		}
		
		return $user_id_array;
	}
	
	/**
	 * Suppose to retrieves the most active users on the site. For now, it just retrieves a random set.
	 * @param $limit
	 * @return array of users
	 */
	public static function retrieveMostActive($limit = null) {
		$c = new Criteria();
		if($limit) {
			$c->setLimit($limit);
		}
		return UserPeer::doSelect($c);
	}
		

	public static function doDeleteAll($con = null)
	{
		if (file_exists($index = Search::getLuceneIndexFile(Search::USER))) {
    			sfToolkit::clearDirectory($index);
    			rmdir($index);
  		}
		return parent::doDeleteAll($con);
	}
	
	public static function createRandomPassword() {
	
	    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
	    srand((double)microtime()*1000000);
	    $i = 0;
	    $pass = '' ;
	    while ($i <= 7) {
	
	        $num = rand() % 33;
	        $tmp = substr($chars, $num, 1);
	        $pass = $pass . $tmp;
	        $i++;
	    }
	    return $pass;
	}
	
}
